package com.school_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school_admin.config.Result;
import com.school_admin.entity.Message;
import com.school_admin.service.impl.MessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageServiceImpl messageService;

    @GetMapping("list")
    public Result<Map> queryMessage(Integer page, Integer pageSize, String uid) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getUid, uid);
        queryWrapper.orderByDesc(Message::getId);
        Page<Message> pageInfo = new Page<>(page, pageSize);
        IPage<Message> pageModel = messageService.page(pageInfo, queryWrapper);
        if (pageModel.getTotal() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", pageModel.getRecords());
            map.put("total", pageModel.getTotal());
            return Result.success(map);
        }
        return Result.fail("通知为空");
    }
    // 删除通知
    @DeleteMapping("remove/{id}")
    public Result<String> removeMessage(@PathVariable("id") Integer id) {
        boolean remove = messageService.removeById(id);
        if (remove) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

}
